package com.zlsy.thread.threadsafe;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhouliang
 * @date 2020/03/19
 **/
public class ReleaseEscapeThread1 {

    private Map<String, String> map;

    public ReleaseEscapeThread1() {
        map = new HashMap<>();
        map.put("021", "上海市");
        map.put("022", "北京市");
        map.put("023", "天津市");
    }

    /***
     * 逸出： 把一个声明为private的对象直接暴露出去，外部其他类可以直接操作，并且修改这个值
     * @return
     */
    public Map<String, String> getMap() {
        return map;
    }

    /**
     * 防止逸出
     *
     * @return
     */
    public Map<String, String> getMapBySafe() {
        return new HashMap<>(map);
    }


    public static void main(String[] args) {
        ReleaseEscapeThread1 releaseEscapeThread1 = new ReleaseEscapeThread1();

        //测试正确发布private的方法

//        Map<String, String> mapBySafe = releaseEscapeThread1.getMapBySafe();
//        mapBySafe.remove("021");
//
//        Map<String, String> map1 = releaseEscapeThread1.map;
//        String s = map1.get("021");
//        System.out.println(s);


        //测试逸出
        Map<String, String> map = releaseEscapeThread1.getMap();
        map.remove("021");
        Map<String, String> map2 = releaseEscapeThread1.map;
        String s1 = map2.get("021");
        System.out.println(s1);
    }
}
